package com.xg.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xg.frame.shiro.JwtUtil;
import com.xg.sys.dto.SysLogDto;
import com.xg.sys.dto.SysLogVo;
import com.xg.sys.service.SysLogService;
import com.xg.sys.vo.PageVO;
import com.xg.frame.constant.UrlConstant;
import com.xg.frame.entity.R;
import com.xg.frame.log.annotation.LogAnnotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 
 * @since 2020-07-27
 */
@Slf4j
@Api(tags = "系统日志管理")
@Controller
@RequestMapping(UrlConstant.WEB_ADMIN_URL +"/log")
public class SysLogController {

    @Autowired
    SysLogService sysLogService;

    @GetMapping
    public String index(Model model) throws Exception {
        return "/sys/sysLog/list";
    }

    @GetMapping(value = "/query")
    @ResponseBody
    public PageVO<SysLogVo> query(String userName, String methodName, String startTime, String endTime,String status, Page<SysLogVo> page) throws Exception {
        final SysLogDto sysLogDto = new SysLogDto();
        sysLogDto.setCreateUserName(userName);
        sysLogDto.setMethodsName(methodName);
        sysLogDto.setStatus(status);
        sysLogDto.setsDate(startTime);
        sysLogDto.seteDate(endTime);
        sysLogService.pageList(page,sysLogDto);
        return new PageVO<SysLogVo>()
                .setTotal(page.getTotal())
                .setRows(page.getRecords());
    }


    /*@ApiOperation(value = "数据字典列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1",paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size",value = "每页显示条数", required = true, defaultValue = "10",paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "createUserName",value = "操作人名称",paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "methodsName",value = "操作功能",paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sDate",value = "起始时间",paramType = "query", dataType = "String",defaultValue="2020-07-01"),
            @ApiImplicitParam(name = "eDate",value = "截止时间",paramType = "query", dataType = "String",defaultValue="2020-07-01"),
            @ApiImplicitParam(name = "status",value = "执行状态",paramType = "query", dataType = "String",defaultValue="0")
    })
    @GetMapping("/page")
    public R<Page<SysLogVo>> getList(String  createUserName, String methodsName, String status, Integer current, Integer size, String sDate, String eDate) throws Exception{

        SysLogDto sysLog = new SysLogDto();
        Page<SysLogVo> page =  new Page(current, size);
        sysLog.setCreateUserName(createUserName);
        sysLog.setMethodsName(methodsName);
        //日期格式化
        String s_date = VerifyParamsUtils.formatDate(sDate);
        String e_date = VerifyParamsUtils.formatDateAddDays(eDate,1);
        sysLog.seteDate(e_date);
        sysLog.setsDate(s_date);
        sysLog.setStatus(status);
        sysLogService.pageList(page,sysLog);
        return R.ok().info(page);
    }*/


    @ApiOperation("日志测试")
    @LogAnnotation(module= UrlConstant.busCenter,recordRequestParam=false,describe="日志测试")
    @GetMapping("/test")
    public R getTest(){
        String token = JwtUtil.sign("1");
        String userId = JwtUtil.getUserId("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjIxNDk4MjU1LCJleHAiOjE2MjIxMDMwNTV9.kSlbr4UcibAxlJL48dlRGcHhNpUA7y0OA7lBesiedHMR6PVgXjPbmq2fLr9AI_X2mcEdsFVzmUQetAlAQ9XDBA");
        log.info("token = {}",token);
        log.info("userId = {}",userId);
        log.debug("debug");
        log.info("info");
        log.error("error");
        return R.ok();
    }
}

